import axios from 'axios';
import { getToken } from '../util';
import { Message } from 'element-ui';
const  PRODUCT_URL = 'http://wxapp.fitbarfitbar.com/apis'; // 生产
const DEVELOPMENT_URL = 'http://192.168.1.115:8080'; // 开发
const TEST_URL = 'http://wxapp.fitbarfitbar.com/testapis'; // 测试
const requestOption = {
  // baseURL: TEST_URL
};

const request = axios.create(requestOption);
const requestHasToken = axios.create(requestOption);
requestHasToken.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Bearer ' + getToken()
  return config
}, err =>{
  return Promise.reject(err)
});
requestHasToken.interceptors.response.use(res => {
  if (res.data.code !== 1) {
    Message.error(res.data.msg || '服务异常!');
    return Promise.reject(new Error(res.data.msg || '服务异常!'))
  } else {
    return res
  }
})

export {
  request, requestHasToken, PRODUCT_URL
}
